/*
 *  Obstacle.cpp
 *  Untitled
 *
 *  Created by Guo Wei Tan on 3/5/14.
 *  Copyright 2014 __MyCompanyName__. All rights reserved.
 *
 */

#include "Obstacle.h"

//========================== C O N S T R U C T O R ==============================
Obstacle::Obstacle()
{
}

//========================== D E S T R U C T O R ==============================
Obstacle::~Obstacle()
{
}

//========================== S T A T I C   F U N C T I O N S ==============================
/*!
    @function createCat
    @param		filename: char*
    			filename of the sprite sheet of the cat
    @param		pMap:  GameMap*
    			a reference to the game map this cat is running on
    @result		returns a cat with loaded sprites
 */
Obstacle* Obstacle::createObstacle(char* filename, float scaleFactor)
{
	Obstacle* obstacle = new Obstacle();

	if( obstacle && obstacle->initWithSpriteFrame(CCSpriteFrame::create(filename, randomObstacleRect())))
	{
		obstacle->setup(scaleFactor);
		obstacle->setScale(scaleFactor); //TODO: to change
		obstacle->autorelease();
		return obstacle;
	}

	CC_SAFE_DELETE(obstacle);
	return NULL;
}

//========================== P R I V A T E   F U N C T I O N S ==============================
CCRect Obstacle::randomObstacleRect()
{
	//get a random obstacle
	int randomWidth = arc4random() % SPRITE_SHEET_WIDTH_COUNT;
	int randomHeight = arc4random() % SPRITE_SHEET_HEIGHT_COUNT;

	//return the rect of this randomed obstacle
	return CCRect(randomWidth * OBSTACLE_WIDTH, randomHeight * OBSTACLE_HEIGHT, OBSTACLE_WIDTH, OBSTACLE_HEIGHT);
}

void Obstacle::setup(float scaleFactor)
{
	actualRectBufferRadius = RECT_BUFFER_RADIUS * scaleFactor;
}

//========== GETTERS ==========
/*!
    @function getRect
    @result		returns a CCRect based on this sprite with a reduced size according to the RECT_BUFFER_RADIUS.
    			mainly used for a more accurate collision detection
 */
CCRect Obstacle::getRect()
{
	return CCRectMake(this->getPosition().x - (this->getContentSize().width/2) + actualRectBufferRadius,
							this->getPosition().y - (this->getContentSize().height/2) + actualRectBufferRadius,
							this->getContentSize().width - actualRectBufferRadius *2,
							this->getContentSize().height - actualRectBufferRadius *2);
}

